let count = 5//每页条数
let page = 1//当前页码
let maxPage//最大页码
let pageArr = [10, 15, 20, 25, 30, 35];//下拉显示页码
getData()
function getData() {
  $.ajax({
    url: '/get',
    method: 'get',
    data: {
      count: count,
      page: page
    },
    success: (res) => {
      console.log(res);
      if (res.error) {
        console.log(res.error)
      } else {
        $('tbody').html('');
        $(res.data).each((i, v) => {
          //数据渲染
          $('tbody').append(`
          <tr data-id="${v.zhaopin_id}">
              <td>${v.zhaopin_id}</td>
              <td>${v.zhaopin_zw}</td>
              <td>${v.zhaopin_rs}</td>
              <td>${v.zhaopin_yx}</td>
              <td>${v.zhaopin_yy}</td>
              <td>${v.zhaopin_yuex}</td>
              <td>${v.zhaopin_xb}</td>
              <td>${v.zhaopin_sqrq}</td>
              <td>${v.zhaopin_dgrq}</td>
              <td>${v.zhaopin_xq}</td>
              <td>${v.zhaopin_cjr}</td>
              <td>${v.zhaopin_cjsj}</td>
              <td>${v.zhaopin_gxr}</td>
              <td>
                <button class="btn_del btn_frameless"><span class="iconfont icon-lajitong"></span>删除</button>
                <button class="btn_com btn_frameless" data-toggle="modal" data-target="#myModal"><span class="iconfont icon-pen"></span>修改</span></button>
              </td>
            </tr>
          `)
        })
        // for (let i = 0; i < Math.ceil((res.total / count)); i++) {
        //   $('.pagination>li:nth-of-type(1)').append(`
        //   <li><a href="#">${i + 1}</a></li>
        //   `)
        // }
        $('.pagination').html(`     
                          <li id="page_left">
                              <a href="#" aria-label="Previous">
                                  <span aria-hidden="true">&laquo;</span>
                              </a>
                          </li>
                          <li id="page_right">
                              <a href="#" aria-label="Next">
                                  <span aria-hidden="true">&raquo;</span>
                              </a>
                          </li>
                          `);
        maxPage = Math.ceil((res.total / count));
        paging('#page_right', maxPage, page)
      }
    }
  })
}
$('.pagination').on('click', '.new_page', function () {
  page = parseInt($(this).children().text());
  $('.new_page').eq(page - 1).toggleClass("active");
  getData();
});
// 上一页
$('.pagination').on('click', '#page_left', function () {
  page > 1 ? page-- : page = 1
  $('.new_page').eq(page - 1).toggleClass("active");
  getData();
});
// 下一页
$('.pagination').on('click', '#page_right', function () {
  page < maxPage ? page++ : page = maxPage
  $('.new_page').eq(page - 1).toggleClass("active");
  getData();
});

//渲染每页多少条下拉框
window.onload = () => {
  pageArr.forEach(item => {
    $('#tiaoshu').append(
      `
      <li data-num="${item}">
                   <a href="#">每页显示 ${item} 条</a>
                </li> 
      `
    )
  })
}

$('.dropdown-menu').on('click', 'li', function () {
  count = parseInt($(this).attr('data-num'));
  getData();
});
//下拉框搜索
$('#yuanxi').click(function () {
  cont = $('#yuanxi option:selected').val();
  console.log(cont)
  $.ajax({
    url: '/chaxun',
    method: 'get',
    data: {
      zhaopin_yx: $('#yuanxi option:selected').val(),
      count: count,
      page: page

    },
    success: (res) => {
      console.log(res)
      if (res.error) {
        console.log(res.error)
      } else {
        $('tbody').html('');
        $(res.data).each((i, v) => {
          //数据渲染
          $('tbody').append(`
          <tr>
              <td>${v.zhaopin_id}</td>
              <td>${v.zhaopin_zw}</td>
              <td>${v.zhaopin_rs}</td>
              <td>${v.zhaopin_yx}</td>
              <td>${v.zhaopin_yy}</td>
              <td>${v.zhaopin_yuex}</td>
              <td>${v.zhaopin_xb}</td>
              <td>${v.zhaopin_sqrq}</td>
              <td>${v.zhaopin_dgrq}</td>
              <td>${v.zhaopin_xq}</td>
              <td>${v.zhaopin_cjr}</td>
              <td>${v.zhaopin_cjsj}</td>
              <td>${v.zhaopin_gxr}</td>
              <td>
                <button class="btn_del btn_frameless"><span class="iconfont icon-lajitong"></span>删除</button>
                <button class="btn_com btn_frameless"data-toggle="modal" data-target="#myModal"><span class="iconfont icon-pen"></span>修改</button>
              </td>
            </tr>
          `)
        })
        $('.pagination').html(`     
                          <li id="page_left">
                              <a href="#" aria-label="Previous">
                                  <span aria-hidden="true">&laquo;</span>
                              </a>
                          </li>
                          <li id="page_right">
                              <a href="#" aria-label="Next">
                                  <span aria-hidden="true">&raquo;</span>
                              </a>
                          </li>
                          `);
        maxPage = Math.ceil((res.total / count));
        console.log(maxPage, 1);
        // paging('#page_right', maxPage, page)
        for (let i = 1; i <= maxPage; i++) {
          $('#page_right').before(`
                      <li class="page_num"><a href="#">${i}</a></li>
                  `);
          if (page == i) {
            $('.page_num').eq(i - 1).toggleClass("active");
          }
        };
        $('.pagination').css({
          display: 'none'
        });
      }
    }
  })
})
//性别查询
$('#xingbie').click(function () {
  cont = $('#xingbie option:selected').val();
  console.log(cont)
  $.ajax({
    url: '/chaxun1',
    method: 'get',
    data: {
      zhaopin_xb: $('#xingbie option:selected').val(),
      count: count,
      page: page

    },
    success: (res) => {
      console.log(res)
      if (res.error) {
        console.log(res.error)
      } else {
        $('tbody').html('');
        $(res.data).each((i, v) => {
          //数据渲染
          $('tbody').append(`
          <tr>
              <td>${v.zhaopin_id}</td>
              <td>${v.zhaopin_zw}</td>
              <td>${v.zhaopin_rs}</td>
              <td>${v.zhaopin_yx}</td>
              <td>${v.zhaopin_yy}</td>
              <td>${v.zhaopin_yuex}</td>
              <td>${v.zhaopin_xb}</td>
              <td>${v.zhaopin_sqrq}</td>
              <td>${v.zhaopin_dgrq}</td>
              <td>${v.zhaopin_xq}</td>
              <td>${v.zhaopin_cjr}</td>
              <td>${v.zhaopin_cjsj}</td>
              <td>${v.zhaopin_gxr}</td>
              <td>
                <button class="btn_del btn_frameless"><span class="iconfont icon-lajitong"></span>删除</button>
                <button class="btn_com btn_frameless"data-toggle="modal" data-target="#myModal"><span class="iconfont icon-pen"></span>修改</button>
              </td>
            </tr>
          `)
        })
        $('.pagination').html(`     
                          <li id="page_left">
                              <a href="#" aria-label="Previous">
                                  <span aria-hidden="true">&laquo;</span>
                              </a>
                          </li>
                          <li id="page_right">
                              <a href="#" aria-label="Next">
                                  <span aria-hidden="true">&raquo;</span>
                              </a>
                          </li>
                          `);
        maxPage = Math.ceil((res.total / count));
        console.log(maxPage, 1);
        // paging('#page_right', maxPage, page)
        for (let i = 1; i <= maxPage; i++) {
          $('#page_right').before(`
                      <li class="page_num"><a href="#">${i}</a></li>
                  `);
          if (page == i) {
            $('.page_num').eq(i - 1).toggleClass("active");
          }
        };
        $('.pagination').css({
          display: 'none'
        });
      }
    }
  })
})
//需求查询
$('#xuqiu').click(function () {
  cont = $('#xuqiu option:selected').val();
  console.log(cont)
  $.ajax({
    url: '/chaxun2',
    method: 'get',
    data: {
      zhaopin_xq: $('#xuqiu option:selected').val(),
      count: count,
      page: page

    },
    success: (res) => {
      console.log(res)
      if (res.error) {
        console.log(res.error)
      } else {
        $('tbody').html('');
        $(res.data).each((i, v) => {
          //数据渲染
          $('tbody').append(`
          <tr data-id="${v.zhaopin_id}">
              <td>${v.zhaopin_id}</td>
              <td>${v.zhaopin_zw}</td>
              <td>${v.zhaopin_rs}</td>
              <td>${v.zhaopin_yx}</td>
              <td>${v.zhaopin_yy}</td>
              <td>${v.zhaopin_yuex}</td>
              <td>${v.zhaopin_xb}</td>
              <td>${v.zhaopin_sqrq}</td>
              <td>${v.zhaopin_dgrq}</td>
              <td>${v.zhaopin_xq}</td>
              <td>${v.zhaopin_cjr}</td>
              <td>${v.zhaopin_cjsj}</td>
              <td>${v.zhaopin_gxr}</td>
              <td>
                <button class="btn_del btn_frameless"><span class="iconfont icon-lajitong"></span>删除</button>
                <button class="btn_com btn_frameless"data-toggle="modal" data-target="#myModal"><span class="iconfont icon-pen"></span>修改</button>
              </td>
            </tr>
          `)
        })
        $('.pagination').html(`     
                          <li id="page_left">
                              <a href="#" aria-label="Previous">
                                  <span aria-hidden="true">&laquo;</span>
                              </a>
                          </li>
                          <li id="page_right">
                              <a href="#" aria-label="Next">
                                  <span aria-hidden="true">&raquo;</span>
                              </a>
                          </li>
                          `);
        maxPage = Math.ceil((res.total / count));
        console.log(maxPage, 1);
        // paging('#page_right', maxPage, page)
        for (let i = 1; i <= maxPage; i++) {
          $('#page_right').before(`
                      <li class="page_num"><a href="#">${i}</a></li>
                  `);
          if (page == i) {
            $('.page_num').eq(i - 1).toggleClass("active");
          }
        };
        $('.pagination').css({
          display: 'none'
        });
      }
    }
  })
})
//搜索框查询
$('.aaa>a').click(function () {
  // cd = $('.drop_down_xuqiu').val()
  // console.log(cd)
  $.ajax({
    url: '/input',
    method: 'get',
    data: {
      zhaopin_zw: $('.drop_down_xuqiu').val(),
      count: count,
      page: page
    },
    success: (res) => {
      console.log(res)
      if (res.error) {
        console.log(res.error)
      } else {
        $('tbody').html('')
        $(res.data).each((i, v) => {
          $('tbody').append(`
          <tr data-id="${v.zhaopin_id}">
              <td>${v.zhaopin_id}</td>
              <td>${v.zhaopin_zw}</td>
              <td>${v.zhaopin_rs}</td>
              <td>${v.zhaopin_yx}</td>
              <td>${v.zhaopin_yy}</td>
              <td>${v.zhaopin_yuex}</td>
              <td>${v.zhaopin_xb}</td>
              <td>${v.zhaopin_sqrq}</td>
              <td>${v.zhaopin_dgrq}</td>
              <td>${v.zhaopin_xq}</td>
              <td>${v.zhaopin_cjr}</td>
              <td>${v.zhaopin_cjsj}</td>
              <td>${v.zhaopin_gxr}</td>
              <td>
                <button class="btn_del btn_frameless"><span class="iconfont icon-lajitong"></span>删除</button>
                <button class="btn_com btn_frameless"data-toggle="modal" data-target="#myModal"><span class="iconfont icon-pen"></span>修改</button>
              </td>
            </tr>
          `)

        })
        $('.pagination').html(`     
                          <li id="page_left">
                              <a href="#" aria-label="Previous">
                                  <span aria-hidden="true">&laquo;</span>
                              </a>
                          </li>
                          <li id="page_right">
                              <a href="#" aria-label="Next">
                                  <span aria-hidden="true">&raquo;</span>
                              </a>
                          </li>
                          `);
        maxPage = Math.ceil((res.total / count));
        console.log(maxPage, 1);
        // paging('#page_right', maxPage, page)
        for (let i = 1; i <= maxPage; i++) {
          $('#page_right').before(`
                      <li class="page_num"><a href="#">${i}</a></li>
                  `);
          if (page == i) {
            $('.page_num').eq(i - 1).toggleClass("active");
          }
        };
        $('.pagination').css({
          display: 'none'
        });

      }
    }
  })
})
//删除
$('tbody').on('click', '.btn_del', function () {
  // console.log($(this).parents('tr').attr('data-id'));
  if(confirm('确定要删除吗？')){
    $.ajax({
      url: '/del',
      method: 'post',
      data: {
        zhaopin_id: $(this).parents('tr').attr('data-id')
      },
      success: (res) => {
        console.log(res);
        if (res.reeor) {
          console.log(res.error)
        } else {
          alert('删除成功')
          getData();
        }
      }
    })
  }
  
})
//修改
$('tbody').on('click','.btn_com',function(){
  console.log($(this).parents('tr').attr('data-id'));
  console.log($(this))
 
})
$('#edit').click(function(){
  cd = $('#zhiwei option:selected').val()
  console.log(cd)
  $.ajax({
    url: '/edit',
    method: 'post',
    data: {
      zhaopin_id: $('.btn_com').parents('tr').attr('data-id'),
      zhaopin_zw: $('#zhiwei option:selected').val(),
      zhaopin_rs: $('#renshu').val(),
      zhaopin_yx: $('#suoshuyuanxi option:selected').val(),
      zhaopin_yy: $('#yuyan option:selected').val(),
      zhaopin_yuex: $('#yuexin option:selected').val(),
      zhaopin_xb: $('#xinbie option:selected').val(),
      zhaopin_sqrq: $('#shenqing').val(),
      zhaopin_dgrq: $('#daogang').val(),
      zhaopin_xq: $('#zhuangtai option:selected').val(),
      zhaopin_cjr: $('#chuangjianren').attr('placeholder'),
      zhaopin_cjsj: $('#chuangjian').val(),
      zhaopin_gxr: $('#gengxinren').attr('placeholder')
    },
    success: (res) => {
      console.log(res)
      if (res.error) {
        console.log(res.error)
      }else{
        alert('修改成功')
        getData()
        
      }
    }
  })
})

//添加

$('.new_btn>button:nth-of-type(1)').click(function () {
  $('#xuqiu').val();
  $('#yuanxi').val()
  $('#zhiwei').val()
  $('#daogang').val()
  cd = $('#yuyan option:selected').val()
  $('#chuangjianren').attr('placeholder')
  $('#gengxinren').attr('placeholder')
  $('#renshu').val()
  $('#xinbie option:selected').val()
  $('#yuexin option:selected').val()
  $('#zhuangtai option:selected').val()
  $('#shenqing').val()
  $('#chuangjian').val()
  // $('#gengxinshijian').val()
  console.log(cd);
  $.ajax({
    url: '/add',
    method: 'post',
    data: {
      zhaopin_id: $('#xuqiu').val(),
      zhaopin_zw: $('#zhiwei option:selected').val(),
      zhaopin_rs: $('#renshu').val(),
      zhaopin_yx: $('#suoshuyuanxi option:selected').val(),
      zhaopin_yy: $('#yuyan option:selected').val(),
      zhaopin_yuex: $('#yuexin option:selected').val(),
      zhaopin_xb: $('#xinbie option:selected').val(),
      zhaopin_sqrq: $('#shenqing').val(),
      zhaopin_dgrq: $('#daogang').val(),
      zhaopin_xq: $('#zhuangtai option:selected').val(),
      zhaopin_cjr: $('#chuangjianren').attr('placeholder'),
      zhaopin_cjsj: $('#chuangjian').val(),
      zhaopin_gxr: $('#gengxinren').attr('placeholder')
    },
    success: (res) => {
      console.log(res)
      if (res.error) {
        console.log(res.error)
      }else{
        alert('添加成功')
        getData()
        
      }
    }
  })

})